// pages/user/account/accountRecordHistory/accountRecordHistory.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recordType: 1,
    recordList: [],
    nowPage: 0,
    isLoading: false,
    isAll: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getRecordList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.getRecordList()
  },

  navClick: function(e) {
    if (this.data.recordType == e.currentTarget.dataset.type) {
      return
    }
    this.setData({
      recordType: e.currentTarget.dataset.type,
      recordList: [],
      nowPage: 0,
      isLoading: false,
      isAll: false
    })
    this.getRecordList()
  },

  getRecordList: function() {
    if (this.data.isLoading) {
      return
    }
    if (this.data.isAll) {
      return
    }
    const that = this
    let recordList = this.data.recordList
    this.setData({
      isLoading: true
    })
    wx.showLoading({
      title: '加载中',
    })
    if (this.data.recordType == 1) {
      let data = {
        type: 1,
        limit: 10,
        page: this.data.nowPage
      }
      common.post('Wallet/user_money_old', data).then(function(res) {
        wx.hideLoading()
        that.setData({
          isLoading: false
        })
        if (res.code == 204) {
          that.setData({
            recordList: recordList,
            isAll: true
          })
        } else {
          recordList = recordList.concat(res)
          that.setData({
            recordList: recordList,
            nowPage: that.data.nowPage + 1
          })
        }
      }).catch(function(e) {})
    } else {
      let data = {
        type: 2,
        limit: 10,
        page: this.data.nowPage
      }
      common.post('Wallet/user_money_old', data).then(function(res) {
        wx.hideLoading()
        that.setData({
          isLoading: false
        })
        if (res.code == 204) {
          that.setData({
            recordList: recordList,
            isAll: true
          })
        } else {
          recordList = recordList.concat(res)
          that.setData({
            recordList: recordList,
            nowPage: that.data.nowPage + 1
          })
        }
      }).catch(function(e) {})
    }
  }
})